/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.common.json;

import com.entities.Employee;
import com.entities.EventInfo;
import com.entities.EventLibrary;
import com.entities.Faq;
import com.entities.EventRegistration;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

/**
 *
 * @author Tuan Long
 */
public class JsonModel {

    /**
     * @JsonModel.convertDatetimeToString: output: string date with format:
     * 
     * @return String
     * @param time Date
     */
    public static String convertDatetimeToString(Date time) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String result = df.format(time);
        return result;
    }

    /**
     * @JsonModel.convertDatetimeToString: output: string date with format:
     *
     * @return String
     * @param time Date
     */
    public static String convertDatetimeFullInfoToString(Date time) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String result = df.format(time);
        return result;
    }

    /**
     * @JsonModel.convertStringToDatetime
     * @return Date
     * @param text String
     */
    public static Date convertStringToDatetime(String text) {
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
            Date date = df.parse(text);
            return date;
        } catch (ParseException ex) {
            Logger.getLogger(JsonModel.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    /**
     * @JsonModel.convertStringToDatetime
     * @return Date
     * @param text String
     */
    public static Date convertStringToSmallDatetime(String text) {
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            Date date = df.parse(text);
            return date;
        } catch (ParseException ex) {
            Logger.getLogger(JsonModel.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    /**
     * @JsonModel.getCurrentDatetime: 
     *   
     * @return  Date
     */
    public static Date getCurrentDatetime() {
        try {
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyy:MM:dd HH:mm:ss");
            String current = sdf.format(date);
            return sdf.parse(current);
        } catch (ParseException ex) {
            Logger.getLogger(JsonModel.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    /**
     * @JsonModel.eventMessageSender : input is EventLibrary object, output is
     * JsonObject.
     * @param obj
     * @return JSONObject
     */
    public static JSONObject eventMessageSender(EventLibrary obj) {
        String start = convertDatetimeToString(obj.getTimeStart());
        String end = convertDatetimeToString(obj.getTimeEnd());
        JSONObject message = new JSONObject();
        message.put("eventId", obj.getEventId());
        message.put("name", obj.getName());
        message.put("description", obj.getDescription());
        message.put("eventType", obj.getEventType());
        message.put("start", start);
        message.put("end", end);
        return message;
    }

    /**
     * @JsonModel.eventsDataTableMessageSender : input is EventLibrary object
     * and number employees were registered , output is JsonObject. (for method:
     * EventLibraryFacade.fillEventsDataTable())
     * @param obj
     * @param registered
     * @return JSONObject
     */
    public static JSONObject eventsDataTableMessageSender(EventLibrary obj, int registered) {
        String start = convertDatetimeToString(obj.getTimeStart());
        String end = convertDatetimeToString(obj.getTimeEnd());
        JSONObject message = new JSONObject();
        message.put("eventId", obj.getEventId());
        message.put("name", obj.getName());
        message.put("description", obj.getDescription());
        message.put("eventType", obj.getEventType());
        message.put("start", start);
        message.put("end", end);
        message.put("registered", registered);
        return message;
    }

    /**
     * @JsonModel.eventMessageReader : input is JsonObject, output is
     * EventLibrary object.
     * @param json
     * @return EventLibrary
     */
    public static EventLibrary eventMessageReader(JSONObject json) {
        String idstr = json.get("eventId").toString();
        int id = Integer.parseInt(idstr);
        String name = json.get("name").toString();
        String des = json.get("description").toString();
        String type = json.get("eventType").toString();
        String start = json.get("start").toString();
        String end = json.get("end").toString();
        Date timeS = convertStringToDatetime(start);
        Date timeE = convertStringToDatetime(end);
        EventLibrary obj = new EventLibrary(id, name, des, type, timeS, timeE);
        return obj;
    }
    /**
     * @JsonModel.eventInfoReader 
     * @param json
     * @return EventInfo
     */
    public static EventInfo eventInfoReader(JSONObject json) {
        String avatar = json.get("avatar").toString();
        String shortdes = json.get("shortdes").toString();
        String address = json.get("address").toString();
        EventInfo ei = new EventInfo();
        ei.setAvatarImg(avatar);
        ei.setEventShortDesc(shortdes);        
        ei.setAddress(address);
        return ei;
    }

    /**
     * @JsonModel.eventMessageSender : input is EventLibrary object, output is
     * JsonObject.
     * @param obj
     * @return JSONObject
     */
    public static JSONObject faqMessageSender(Faq obj) {
        JSONObject message = new JSONObject();
        message.put("faqId", obj.getFaqId());
        message.put("question", obj.getQuestion());
        message.put("answer", obj.getAnswer());
        return message;
    }

    /**
     * @JsonModel.eventMessageReader : input is JsonObject, output is
     * EventLibrary object.
     * @param json
     * @return EventLibrary
     */
    public static Faq faqMessageReader(JSONObject json) {
        String idstr = json.get("faqId").toString();
        int id = Integer.parseInt(idstr);
        String question = json.get("question").toString();
        String answer = json.get("answer").toString();
        Faq obj = new Faq(id, question, answer);
        return obj;
    }

    /**
     * @JsonModel.JsonDecoder : input is String values send by client
     * @param jsonstr
     * @return JSONObject
     */
    public static JSONObject JsonDecoder(String jsonstr) {
        try {
            JSONParser parser = new JSONParser();
            JSONObject json = (JSONObject) parser.parse(jsonstr);
            return json;
        } catch (org.json.simple.parser.ParseException ex) {
            Logger.getLogger(JsonModel.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    /**
     * @JsonModel.eventRegistrationSender : convert The data of select by stored
     * procedure: sp_fillRegisteredTable To JSONObject
     * @param er
     * @param eventname
     * @return JSONObject
     */
    public static JSONObject eventRegistrationSender(EventRegistration er, String eventname) {
        JSONObject message = new JSONObject();
        String registered = convertDatetimeFullInfoToString(er.getTimeRegisted());
        String username = er.getUsername().getUsername();
        message.put("eventname", eventname);
        message.put("username", username);
        message.put("registered", registered);
        return message;
    }

    /**
     * @param username
     * @param total
     * @return JSONObject
     */
    public static JSONObject eventTotalRegistrationSender(String username, String total) {
        JSONObject message = new JSONObject();
        message.put("username", username);
        message.put("total", total);
        return message;
    }

    /**
     * @param eventtype String
     * @param total_str String
     * @return JSONObject
     */
    public static JSONObject eventTotalEventTypeSender(String eventtype, String total_str) {
        JSONObject message = new JSONObject();
        message.put("eventtype", eventtype);
        message.put("total", total_str);
        return message;
    }

    /**
     * @param eventID String
     * @param name String
     * @param user String
     * @param comment String
     * @param commented Date
     * @return JSONObject
     */
    public static JSONObject readEventCommentsSender(String eventID, String name, String user, String comment, Date commented) {
        JSONObject message = new JSONObject();
        String time = convertDatetimeToString(commented);
        message.put("eventID", eventID);
        message.put("eventName", name);
        message.put("usercomment", user);
        message.put("comment", comment);
        message.put("commented", time);
        return message;
    }

    /**
     * @param id integer
     * @param name String
     * @param des String
     * @param type Strung
     * @param timeS Date
     * @param timeE Date
     * @param comments Integer
     * @return JSONObject
     */
    public static JSONObject getEventContentByIdSender(int id, String name, String des, String type, Date timeS, Date timeE, int comments) {
        JSONObject message = new JSONObject();
        String start = convertDatetimeToString(timeS);
        String end = convertDatetimeToString(timeE);
        String eventID = "" + id;
        String count = "" + comments;
        message.put("eventID", eventID);
        message.put("eventName", name);
        message.put("eventType", type);
        message.put("description", des);
        message.put("start", start);
        message.put("end", end);
        message.put("comments", count);
        return message;
    }
    
    public static JSONObject getTop10EventIdAndNameSender(int id, String eventName) {
        JSONObject message = new JSONObject();
        String eventID = "" + id;
        message.put("eventID", eventID);
        message.put("eventName", eventName);
        return message;
    }

    public static String getUpdateEventInformationByIdSender(int id, String name, String des, String type, Date timeS, Date timeE, String avatar, String shortdes, Date created, String address) {
        JSONObject message = new JSONObject();
        String start = convertDatetimeToString(timeS);
        String end = convertDatetimeToString(timeE);
        String timeCreated = convertDatetimeFullInfoToString(created);
        String eventID = "" + id;
        message.put("eventID", eventID);
        message.put("eventName", name);
        message.put("eventType", type);
        message.put("description", des);
        message.put("start", start);
        message.put("end", end);
        message.put("avatar", avatar);
        message.put("shortDes", shortdes);
        message.put("created", timeCreated);     
        message.put("address", address);
        return message.toString();
    }
    public static String getEventCommingSender(int id, String name, String des, String type, Date timeS, Date timeE, String avatar, String shortdes, Date created, String address,int total) {
        JSONObject message = new JSONObject();
        String start = convertDatetimeToString(timeS);
        String end = convertDatetimeToString(timeE);
        String timeCreated = convertDatetimeFullInfoToString(created);
        String eventID = "" + id;
        String totalstr=""+total;
        message.put("eventID", eventID);
        message.put("eventName", name);
        message.put("eventType", type);
        message.put("description", des);
        message.put("start", start);
        message.put("end", end);
        message.put("avatar", avatar);
        message.put("shortDes", shortdes);
        message.put("created", timeCreated);     
        message.put("address", address);
        message.put("total", totalstr);
        return message.toString();
    }

    public static String getEventContentById2Sender(EventInfo ei) {
        JSONObject message = new JSONObject();
        String created = convertDatetimeFullInfoToString(ei.getCreated());
        message.put("avatar", ei.getAvatarImg());
        message.put("shortdes", ei.getEventShortDesc());
        message.put("created", created);
        message.put("address", ei.getAddress());        
        return message.toString();
    }

    public static String getJsonEmployeeInfoSender(Employee em) {
        JSONObject message = new JSONObject();
        String birthday = convertDatetimeToString(em.getBirthdate());
        String joined = convertDatetimeFullInfoToString(em.getJoinInDate());
        String lastlogged = convertDatetimeFullInfoToString(em.getLogged());
        String online = em.getOnline().toString();
        message.put("Username", em.getUsername());
        message.put("Password", em.getPassword());
        message.put("Email", em.getEmail());
        message.put("Fullname", em.getFullname());
        message.put("Gender", em.getGender());
        message.put("Birthdate", birthday);
        message.put("Phone", em.getPhone());
        message.put("Mobilephone", em.getMobilephone());
        message.put("Address", em.getAddress());
        message.put("IdentityCard", em.getIdentityCard());
        message.put("JoinInDate", joined);
        message.put("Position", em.getPosition());
        message.put("Departments", em.getDepartments());
        message.put("Groups", em.getGroups());
        message.put("Status", em.getStatus());
        message.put("Online", online);
        message.put("Logged", lastlogged);
        message.put("Avatar", em.getAvatar());
        return message.toString();
                
    }

    public static String getNewMessageSender(int id, String sender, String content, boolean recieved, Date sent) {
       JSONObject message = new JSONObject();
        String mId =""+id;
        String mRecieved = ""+recieved;
        String mdate = convertDatetimeFullInfoToString(sent);
        message.put("id", mId);
        message.put("sender", sender);
        message.put("content", content);
        message.put("recieved", mRecieved);
        message.put("sent", mdate);
        return message.toString();
    }

    public static String getRegistersSender(String username, String fullname, String avatar, String position, String department, Date logged) {
        JSONObject message = new JSONObject();
        String date = convertDatetimeFullInfoToString(logged);
        message.put("username", username);
        message.put("fullname", fullname);
        message.put("avatar", avatar);
        message.put("position", position);
        message.put("department", department);
        message.put("logged", date);
        return message.toString();
    }

}
